<template>
  <div class="article-page">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card shadow="never" border="false">
      <!-- 卡片头部 -->
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            @click="openDrawer('add')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
          >
            添加面经
          </el-button>
        </div>
      </template>
      <!-- 卡片内容 -->
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="stem" label="标题"></el-table-column>
        <el-table-column prop="creator" label="作者"></el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览数"> </el-table-column>
        <el-table-column prop="createdAt" label="更新时间"> </el-table-column>
        <!-- 想让表格中的某一个渲染自定义UI结构，需要给el-table-column组件传递插槽内容 -->
        <el-table-column label="操作">
          <template #default="{ row }">
            <div class="actions">
              <i class="el-icon-view" @click="openDrawer('preview', row)"></i>
              <i
                class="el-icon-edit-outline"
                @click="openDrawer('edit', row)"
              ></i>
              <i class="el-icon-delete" @click="deleteArticle(row)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <!-- layout：指定分页器的布局 -->
      <!-- background：带有背景的分页器 -->
      <!-- current-page：指定分页器在第几页 -->
      <!-- page-size：控制每页展示多少条 -->
      <!-- total：总条数 -->
      <!-- 一共有多少页 = Math.ceil(总条数据 / 每页展示多少条) -->
      <!-- @current-change：当前页发生变化，会触发的事件 -->
      <el-pagination
        @current-change="handleCurrentChange"
        layout="prev, pager, next"
        background
        :current-page="current"
        :page-size="10"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 抽屉组件 -->
    <!-- title：抽屉中的标题 -->
    <!-- visible.sync：用于控制抽屉是否显示 -->
    <!-- direction：控制抽屉打开的方向 -->
    <el-drawer
      :title="drawerTitle"
      :visible.sync="showDrawer"
      :direction="'rtl'"
      size="50%"
      @close="drawerClose"
    >
      <!-- 预览：非表单内容 -->
      <div v-if="type === 'preview'" class="article-preview">
        <h5>{{ ruleForm.stem }}</h5>
        <div v-html="ruleForm.content"></div>
      </div>
      <!-- 添加/编辑：表单内容 -->
      <el-form
        v-else
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="70px"
      >
        <el-form-item label="标题" prop="stem">
          <el-input v-model="ruleForm.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <!-- 富文本编辑器 -->
          <quillEditor
            v-model="ruleForm.content"
            @blur="$refs.ruleForm.validateField('content')"
          ></quillEditor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">确定</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import {
  addArticle,
  deleteArticle,
  editArticle,
  getArticleDetail,
  getArticleList,
} from "@/api/article";
// require styles
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";

import { quillEditor } from "vue-quill-editor";
export default {
  name: "article-page",
  components: {
    quillEditor,
  },
  data() {
    return {
      tableData: [],
      // 分页数据
      current: 1,
      total: 0,
      // 控制抽屉是否显示的数据
      showDrawer: false,
      // 抽屉标题
      drawerTitle: "",
      titleMap: {
        add: "添加面经",
        edit: "修改面经",
        preview: "面经预览",
      },
      // 表单数据对象
      ruleForm: {
        stem: "",
        content: "",
      },
      // 表单验证规则对象
      rules: {
        stem: [{ required: true, message: "请填写面经标题", trigger: "blur" }],
        content: [
          { required: true, message: "请填写面经内容", trigger: "blur" },
        ],
      },
      type: "",
    };
  },
  async created() {
    // 获取文章列表数据
    this.loadArticleList();
  },
  methods: {
    // 获取文章列表数据
    async loadArticleList() {
      const res = await getArticleList({
        current: this.current,
        pageSize: 10,
      });
      this.tableData = res.data.rows;
      this.total = res.data.total;
    },
    async handleCurrentChange(page) {
      this.current = page;
      // 发送请求，获取当前页的表格数据
      this.loadArticleList();
    },
    // 打开抽屉
    async openDrawer(type, row) {
      this.type = type;
      console.log(row);
      // if (type === "add") {
      //   this.drawerTitle = "添加面经";
      // } else if (type === "edit") {
      //   this.drawerTitle = "修改面经";
      // } else {
      //   this.drawerTitle = "面经预览";
      // }
      // 修改抽屉标题
      this.drawerTitle = this.titleMap[type];
      // 打开抽屉
      this.showDrawer = true;

      // 编辑和预览时，需要调用获取文章详情接口
      if (type !== "add") {
        const res = await getArticleDetail(row.id);
        // console.log(res);
        // 数据回显
        this.ruleForm = res.data;
      }
    },
    // 抽屉关闭
    drawerClose() {
      if (this.type !== "preview") {
        // 清空表单内容和校验结果
        this.$refs.ruleForm.resetFields();
      }
    },
    // 添加/修改面经
    submit() {
      // 表单的兜底校验
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.type === "add") {
            // 添加
            // 调用添加面经接口
            await addArticle(this.ruleForm);
            // 提示用户
            this.$message.success("添加成功");
          } else {
            // 编辑
            // 调用修改面经接口
            const { id, stem, content } = this.ruleForm;
            await editArticle({ id, stem, content });
            // 提示用户
            this.$message.success("修改成功");
          }

          // 关闭抽屉
          this.showDrawer = false;
          // 请求第一页的面经文章列表数据
          this.current = 1;
          this.loadArticleList();
        }
      });
    },
    // 删除面经
    async deleteArticle(row) {
      // 调用删除面经接口
      await deleteArticle(row.id);
      // 提示用户
      this.$message.success("删除成功");
      // 调用获取文章列表数据的方法
      this.loadArticleList();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
